#ifndef __MESSAGE_QUEUE_H
#define __MESSAGE_QUEUE_H

#include "../rtx/rtx_os.h"

#define MESSAGE_QUEUE_INFO(NAME, CAPACITY, MSG_TYPE)                           \
    static uint64_t                                                            \
        NAME##_message_queue_cb_mem[(osRtxMessageQueueCbSize + 7) / 8];        \
    static uint64_t NAME##_message_queue_mq_mem                                \
        [(osRtxMessageQueueMemSize(CAPACITY, sizeof(MSG_TYPE)) + 7) / 8];      \
    static osMessageQueueId_t NAME##_message_queue_id;                         \
    static inline void NAME##_message_queue_create() {                         \
        osMessageQueueAttr_t attr = {                                          \
            .name = #NAME,                                                     \
            .attr_bits = 0,                                                    \
            .cb_mem = NAME##_message_queue_cb_mem,                             \
            .cb_size = osRtxMessageQueueCbSize,                                \
            .mq_mem = NAME##_message_queue_mq_mem,                             \
            .mq_size = osRtxMessageQueueMemSize(CAPACITY, sizeof(MSG_TYPE)),   \
        };                                                                     \
        NAME##_message_queue_id =                                              \
            osMessageQueueNew(CAPACITY, sizeof(MSG_TYPE), &attr);              \
    }                                                                          \
    static inline osStatus_t NAME##_message_queue_post(const MSG_TYPE *msg) {  \
        return osMessageQueuePut(NAME##_message_queue_id, msg, 0, 0);          \
    }                                                                          \
    static inline osStatus_t NAME##_message_queue_poll(MSG_TYPE *msg,          \
                                                       uint32_t timeout) {     \
        return osMessageQueueGet(NAME##_message_queue_id, msg, NULL, timeout); \
    }

#endif
